import Vue from 'vue'
import VueRouter from 'vue-router'
import Reg from '@/views/Reg/Reg.vue'
import Login from '@/views/Login/Login.vue'
import Main from '@/views/Main/Main.vue'
import Home from '@/views/Menus/Home/Home.vue'
import UserInfo from '@/views/Menus/User/UserInfo.vue'
import UserAvatar from '@/views/Menus/User/UserAvatar.vue'
import UserPwd from '@/views/Menus/User/UserPwd.vue'

Vue.use(VueRouter)

const routes = [
  { //注册路由配置
    path: '/reg',
    name: 'reg',
    component: Reg
  },
  { //登录路由配置
    path: '/login',
    name: 'login',
    component: Login
  },
  { //首页路由配置
    path: '/',
    name: 'main',
    component: Main,
    redirect: 'home',
    children: [
      {
        //主页路由配置
        path: 'home',
        name: 'home',
        component: Home,
      },
      {
        //用户路由配置
        path: 'user-info',
        name: 'userInfo',
        component: UserInfo,
      },
      {
        //图像路由配置
        path: 'user-avatar',
        name: 'useravatar',
        component: UserAvatar,
      },
      {
        //重置密码路由配置
        path: 'user-pwd',
        name: 'userpwd',
        component: UserPwd,
      },
    ]
  }
]

const router = new VueRouter({
  routes
})

// VueRouter.prototype.push: push函数要求不能重复 跳转路由
// 解决办法，就是改写  VueRouter中的push函数，但需要保留之前的功能
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}

export default router
